package com.ruoyi.jms;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.listener.DefaultMessageListenerContainer;
import org.springframework.jms.listener.SimpleMessageListenerContainer;
import org.springframework.jms.remoting.JmsInvokerProxyFactoryBean;
import org.springframework.jms.remoting.JmsInvokerServiceExporter;
import org.springframework.jndi.JndiObjectFactoryBean;
import org.springframework.jndi.JndiTemplate;

import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.Queue;
import java.util.Properties;

@Configuration
public class JmsConfiguration {
//    private String userName = "weblogic";
//    private String password = "weblogic";
    private String connectionFactory = "jms/jms_test_connection_factory1";
    private String queue = "jms/jms_test_queue";
    private String queue1 = "jms/jms_test_queue1";
    private String url = "t3://localhost:7001";

    @Autowired
    private MyMessageListener myMessageListener;

    @Bean(name = "weblogicJms")
    public JndiTemplate weblogicJms() {
        Properties props = new Properties();
        props.setProperty("java.naming.factory.initial", "weblogic.jndi.WLInitialContextFactory");
        props.setProperty("java.naming.provider.url", url);
        //props.setProperty("java.naming.security.principal", userName);
        //props.setProperty("java.naming.security.credentials", password);
        JndiTemplate jmsJndiTemplate = new JndiTemplate();
        jmsJndiTemplate.setEnvironment(props);
        return jmsJndiTemplate;
    }

    @Bean(name = "jmsConnectionFactory")
    public JndiObjectFactoryBean jmsConnectionFactory() {
        JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();
        jndiObjectFactoryBean.setJndiName(connectionFactory);
        jndiObjectFactoryBean.setJndiTemplate(weblogicJms());
        return jndiObjectFactoryBean;
    }

    @Bean(name = "jmsDestination")
    public JndiObjectFactoryBean jmsDestination() {
        JndiObjectFactoryBean jmsDestination = new JndiObjectFactoryBean();
        jmsDestination.setJndiName(queue);
        jmsDestination.setJndiTemplate(weblogicJms());
        return jmsDestination;
    }

    @Bean(name = "jmsDestination1")
    public JndiObjectFactoryBean jmsDestination1() {
        JndiObjectFactoryBean jmsDestination = new JndiObjectFactoryBean();
        jmsDestination.setJndiName(queue1);
        jmsDestination.setJndiTemplate(weblogicJms());
        return jmsDestination;
    }

    @Bean(name = "listenerQueue1")
    @ConditionalOnMissingBean
    public DefaultMessageListenerContainer listenerQueue1() {
        DefaultMessageListenerContainer listener = new DefaultMessageListenerContainer();
        listener.setConnectionFactory((ConnectionFactory) jmsConnectionFactory().getObject());
        listener.setDestination((Destination) jmsDestination1().getObject());
        listener.setAutoStartup(true);
        listener.setPubSubDomain(true);
        listener.setMessageListener(myMessageListener);
        return listener;
    }

    @Bean(name = "jmsTemplate")
    @ConditionalOnMissingBean
    public JmsTemplate getJmsTemplate() {
        JmsTemplate jmsTemplate = new JmsTemplate();
        jmsTemplate.setConnectionFactory((ConnectionFactory) jmsConnectionFactory().getObject());
        jmsTemplate.setDefaultDestination((Destination) jmsDestination1().getObject());
        jmsTemplate.setPubSubDomain(true);
        jmsTemplate.setExplicitQosEnabled(true);
        return jmsTemplate;
    }

    @Bean(name = "myHelloService")
    public JmsInvokerProxyFactoryBean getJmsInvokerProxyFactoryBean() {
        JmsInvokerProxyFactoryBean myHelloService = new JmsInvokerProxyFactoryBean();
        myHelloService.setConnectionFactory((ConnectionFactory) jmsConnectionFactory().getObject());
        myHelloService.setQueue((Queue) jmsDestination().getObject());
        myHelloService.setServiceInterface(HelloWord.class);
        return myHelloService;
    }

    @Bean(name = "helloService")
    public HelloWordImpl helloService() {
        HelloWordImpl helloService = new HelloWordImpl();
        return helloService;
    }

    @Bean(name = "jmsServiceExporter")
    public JmsInvokerServiceExporter jmsServiceExporter() {
        JmsInvokerServiceExporter jmsInvokerServiceExporter = new JmsInvokerServiceExporter();
        jmsInvokerServiceExporter.setService(helloService());
        jmsInvokerServiceExporter.setServiceInterface(HelloWord.class);
        return jmsInvokerServiceExporter;
    }

    @Bean(name = "simpleMessageListenerContainer")
    public SimpleMessageListenerContainer simpleMessageListenerContainer() {
        SimpleMessageListenerContainer simpleMessageListenerContainer = new SimpleMessageListenerContainer();
        simpleMessageListenerContainer.setConnectionFactory((ConnectionFactory) jmsConnectionFactory().getObject());
        simpleMessageListenerContainer.setDestination((Queue) jmsDestination().getObject());
        simpleMessageListenerContainer.setMessageListener(jmsServiceExporter());
        return simpleMessageListenerContainer;
    }
}
